<template>
  <div class="custom-table-list-wrapper">
    <div class="search" :class="{ active: toggleSearchStatus }">
      <div class="search-left" ref="searchFromRef">
        <el-form class="search-form" inline>
          <el-form-item label="巡检类型">
            <el-select
              class="search-select"
              v-model="searchParams.typeId"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="(item, index) of dict['inspection_inspection_type']"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="计划状态">
            <el-select
              class="search-select"
              v-model="searchParams.status"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="(item, index) of dict['inspection_status']"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="开始日期">
            <el-date-picker
              v-model="searchParams.planBeginTime"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择"
            />
          </el-form-item>
          <el-form-item label="关键词">
            <el-input
              class="search-input"
              v-model="searchParams.keyword"
              placeholder="请输入巡检计划名称、编号"
              :suffix-icon="Search"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
        <a
          class="search-left-toggle"
          v-if="showToggle"
          @click="handleToggleSearch"
        >
          <svg-icon :name="toggleSearchStatus ? 'up' : 'down'" />
          {{ toggleSearchStatus ? '收起' : '展开' }}
        </a>
      </div>
      <div class="search-right">
        <el-button type="primary" @click="initTableList(1)"
          ><svg-icon name="search" size="14px" />搜索</el-button
        >
        <el-button @click="handleReset()"
          ><svg-icon name="reset" size="17px" />重置</el-button
        >
      </div>
    </div>
    <div class="content">
      <div class="content-operate">
        <el-button type="primary" @click="handleAdd('planManageEdit', {}, true)"
          ><svg-icon name="add" size="15px" />新增计划</el-button
        >
      </div>
      <div class="content-table">
        <el-table class="custom-table" :data="tableData">
          <el-table-column type="index" label="序号" width="60px" />
          <el-table-column prop="code" label="巡检计划编号" />
          <el-table-column prop="name" label="巡检计划名称" />
          <el-table-column prop="typeId" label="检查类型">
            <template #default="scope">
              {{ getDictValue('inspection_inspection_type', scope.row.typeId) }}
            </template>
          </el-table-column>
          <el-table-column prop="planBeginTime" label="计划开始日期">
            <template #default="scope">
              {{ moment(scope.row.planBeginTime).format('YYYY-MM-DD') }}
            </template>
          </el-table-column>
          <el-table-column prop="planEndTime" label="计划结束日期">
            <template #default="scope">
              {{ moment(scope.row.planEndTime).format('YYYY-MM-DD') }}
            </template>
          </el-table-column>
          <el-table-column prop="taskTotal" label="任务总数(项)" />
          <el-table-column prop="taskCompleted" label="已完成任务数(项)" />
          <el-table-column prop="status" label="计划状态">
            <template #default="scope">
              <el-tag
                :type="getDictStyle('inspection_status', scope.row.status)"
                >{{
                  getDictValue('inspection_status', scope.row.status)
                }}</el-tag
              >
            </template>
          </el-table-column>
          <!-- <el-table-column prop="isStart" label="是否启动">
            <template #default="scope">
              <el-switch
                :active-value="1"
                :inactive-value="0"
                v-model="scope.row.isStart"
                @change="handleChangeStatus(scope.row)"
                :disabled="
                  [2, 3].includes(scope.row.status) && scope.row.isStart === 1
                "
              />
            </template>
          </el-table-column> -->
          <el-table-column label="操作" width="160px">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="
                  handleDetail(
                    { pkid: scope.row.pkid },
                    'planManageDetail',
                    true,
                  )
                "
                >详情</el-button
              >
              <el-button
                link
                type="primary"
                @click="
                  handleEdit({ pkid: scope.row.pkid }, 'planManageEdit', true)
                "
                v-if="['-1', '0'].includes(scope.row.status)"
                >编辑</el-button
              >
              <el-button
                link
                type="danger"
                @click="handleDelete(scope.row)"
                v-if="['-1', '0'].includes(scope.row.status)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="content-page">
        <el-pagination
          class="custom-pagination"
          :current-page="paginationParams.pageIndex"
          :page-size="paginationParams.pageSize"
          background
          :page-sizes="[10, 20, 30, 50]"
          layout="total, prev, pager, next, sizes, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="initTableList"
        />
      </div>
    </div>
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import { useTableList } from '@/hooks/useTableList'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  getPlanList,
  deletePlan,
} from '@/api/manager/inspectionManage/planManage.js'
import moment from 'moment'
import { useDict } from '@/hooks/useDict'
const { dict, getDictStyle, getDictValue } = useDict([
  'inspection_inspection_type',
  'inspection_status',
])

const searchParams = ref({})
const {
  Search,
  toggleSearchStatus,
  showToggle,
  handleToggleSearch,
  tableData,
  paginationParams,
  total,
  initTableList,
  handleSizeChange,
  handleReset,
  handleDelete,
  addRef,
  detailRef,
  currentDetail,
  handleAdd,
  handleEdit,
  handleDetail,
  handleClose,
  searchFromRef,
} = useTableList(searchParams, getPlanList, deletePlan)

const handleChangeStatus = (val) => {
  ElMessage.success('状态修改成功')
}
onMounted(() => {
  initTableList(1)
})
</script>
<style lang="scss" scoped>
@import '@/assets/styles/table.scss';
</style>
